<?php
header("Content-Type:text/html; charset=utf-8");
date_default_timezone_set('PRC');
require_once('config.php');
$conn = mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);
mysql_select_db($cfg_dbname,$conn);
function download($url,$path)
{
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  $file = curl_exec($ch);
  curl_close($ch);
  $resource = fopen($path, 'a');
  fwrite($resource, $file);
  fclose($resource);
}
function randomkeys($length)   
{   
   //$pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ'; 
      $str = null;
      $strPol = "1234567890";//大小写字母以及数字
      $max = strlen($strPol)-1;
      
      for($i=0;$i<$length;$i++){
         $str.=$strPol[rand(0,$max)];
      }
      return $str;
}
function randomsku($length)   
{   
   $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';  
    for($i=0;$i<$length;$i++)   
    {   
        $key .= $pattern{mt_rand(0,35)};    //生成php随机数   
    }   
    return $key;
}
function trimd($str){
$str=str_replace("\t"," ",$str);
$str=str_replace("'","''",$str);
$str=str_replace('V',"",$str);
$str=str_replace("\r","",$str);
$str=str_replace("\n","<br/>",$str);
return trim($str);
}
$data = json_decode($_POST["data"], true);
$user=trimd($_GET["u"]);
$Platform=$data['Platform'];
$Category=$data['Category'];
$Url=$data['Url'];
$Proname=$data['Proname'];
$Price=$data['Price'];
$Currency=$data['Currency'];
$Shopname=$data['Shopname'];
$Sketch=$data['Sketch'];
$Description=$data['Description'];
$PID=$data['PID'];
$Images=$data['Images'];
$Attribute=$data['Attribute'];
$ucalist="select * from ph_admin where admin='".$user."'";
$ucaresult=mysql_query($ucalist);
if(mysql_num_rows($ucaresult)<1){
echo "user not exist!";
exit;
}
$calist="select * from ph_bcategory where bname='".$Category."' and platform='".$Platform."' and user_name='".$user."'";
$caresult=mysql_query($calist);
if(mysql_num_rows($caresult)<1){
$sqlca = "INSERT INTO ph_bcategory(bname,platform,user_name)VALUES('".$Category."','".$Platform."','".$user."')";
mysql_query($sqlca,$conn);
}
$Arrts=array();
$Arrtsname=array();
foreach ($Attribute as $key=>$Value) {
  $Arrt=array();
  //echo $key."#";
  array_push($Arrtsname,$key);
  foreach ($Value as $mkey) {
    if(empty($mkey['Price'])){$theprice=$Price;}else{$theprice=$mkey['Price'];}
    $theValue=$mkey['Value']."@@".$mkey['Image']."@@".$theprice;
      array_push($Arrt,$theValue);
  }
  array_push($Arrts,$Arrt);
}
if(count($Arrtsname)>1){$shuxing="ColorSize";}else{$shuxing=$Arrtsname[0];}
$sql = "INSERT INTO ph_products(proname,price,saleprice,skucode,coin,code,content,ndate,bname,user_name,des,keywords,weight,kucun,btname,fromurl,shopname,platform,sketch)VALUES('".$Proname."','".$Price."','".$Price."','".randomsku(19)."','".$Currency."','".randomkeys(13)."','".trimd($Description)."','".date("Y-m-d",time())."','".$Category."','".$user."','','','1','50','".trimd($shuxing)."','".$Url."','".$Shopname."','".$Platform."','".$Sketch."')";
mysql_query($sql,$conn);
$proid=mysql_insert_id();

$allproducts = getshuxing($Arrts,$Arrts[0],1);
foreach ($allproducts as $key) {
  $allkey=explode("@@", $key);
  mysql_query("INSERT INTO ph_shuxing(cname,proid,price,saleprice,kucun,skucode)VALUES('".trimd($allkey[0])."','".$proid."','".$Price."','".$Price."','50','');",$conn);
    $btid=mysql_insert_id();
  if(!empty($allkey[1])){
    $pathimg='uploadimg/'.randomkeys(8).strtotime(date("Y-m-d H:i:s",time())).".jpg";
    download($allkey[1],'products/'.$pathimg); 
    mysql_query("INSERT INTO ph_img(imgpath,proid,px,btid)VALUES('".$pathimg."','".$proid."','".$r."','".$btid."');",$conn);
  }
  $r=$r+1;
}
function getshuxing($Arrts,$all=array(''),$curIndex=0){
  $cunt=count($Arrts);
  if($cunt==$curIndex){
    return $all;
  }else{
    $Tempall=array();
    $allcount=count($all);
    $arrt=$Arrts[$curIndex];
    $artcount=count($arrt);
    for($i=0;$i<$artcount;$i++){
      for($ai=0;$ai<$allcount;$ai++){
        array_push($Tempall,$arrt[$i].",".$all[$ai]);
      }
    }
    $curIndex+=1;
    return getshuxing($Arrts,$Tempall,$curIndex);
  }
}
for($i=0;$i<count($Images);$i++){
  $path='uploadimg/'.randomkeys(8).strtotime(date("Y-m-d H:i:s",time())).".jpg";
 download($Images[$i],'products/'.$path);
 mysql_query("INSERT INTO ph_img(imgpath,proid,px)VALUES('".$path."','".$proid."','".$i."');",$conn);
  }
  echo "ok";
?>